<?php
Zend_Loader::loadClass('Led_Entity_View');

Zend_Loader::loadClass('Lead_Markup_Xhtml');

class Led_Entity_Renderer
{
    
    const MODE_RETAIL = 'retail';
    const MODE_PRETAIL = 'pretail';
    const MODE_ADMIN = 'admin';
        
    private $_root;
    

    
    private function __construct()
    {
        $this->_root = dirname(__FILE__);
    }
    
    
    public static function getInstance()
    {
        static $_instance;
        if(!$_instance)
            $_instance = new Led_Entity_Renderer();
                
        return $_instance;
    }
       
    
    public function render(Lead_EDO_Object_Abstract $obj, $mode = self::MODE_RETAIL, $params = array())   
    {
        $view = new Led_Entity_View($this);
        
        $view->entity = $obj;
        $view->params = $params;
        $view->app = Led_Application::getInstance();
        
        $view->mode = $mode; 

        
        $e = false;
        try {
            return $view->render(get_class($obj) . '/' . $mode . '.tpl');    
        }
        catch(Exception $e) {

            // Something smarter here, maybe. This was a quick'n dirty solution.
            if($mode == 'pretail') {
                try {
                    return $view->render(get_class($obj) . '/retail.tpl');
                } catch (Exception $e) {
                    print_r($e->getMessage());
                    return '<em>' . $obj->getClass() . '</em>' . ' cant be rendered...';
                }

            }
        
            
          print_r($e->getMessage());
          return '<em>' . $obj->getClass() . '</em>' . ' cant be rendered...';
            
        }
        
        
    }
    
    
    public function getRoot()
    {
        return $this->_root;
    }
    
    
    
    
    
    
}